<?php
function mosGetImages(& $content, $title, $arr_Images , &$arr_objImage, $path_image , $link_image, $param_getimage =  array())
{	
	if (!is_dir($path_image)) {
		mkdir($path_image);
	}
	$href	=	new href();
	$image_prefix	=	$href->take_file_name($title);	
	$content	=	mosProcessImages($content, $arr_Images, $arr_objImage, $path_image,$image_prefix,$link_image, $param_getimage);		
	return true;
}

function mosProcessImages($content, $arr_Images, &$arr_objImage, $path_image, $image_prefix,$link_image, $param_getimage = array())
{	
	//find all href value in a tag (with ")
	$href	=	new href();
		for ($i = 0; $i < count($arr_Images); $i++){
			$number	=	count($arr_objImage)+1;
			$image_name	=	$image_prefix.'-'.$number;
			
			$obj_get_image	=	new vov_Get_Image($arr_Images[$i],$path_image);
			if (!$response = $obj_get_image->get_image($image_name, $param_getimage)) {
//				$content = str_replace($arr_Images[$i], '', $content);
				continue;
			}
			$_link_image	=	$link_image.$response->file_name;
			$obj_image	=	new stdClass();
			$obj_image->url	=	$_link_image;
			$obj_image->sourceUrl	=	$arr_Images[$i];			
			$obj_image->path		=	$path_image;
			$arr_objImage[]			=	$obj_image;
			$content = str_replace($arr_Images[$i],  $_link_image, $content);
			// insert image
		}
	return $content;
}
//	var $id				=	null;
//	var $cid			=	null;
//	var $title			=	'';
//	var $path			=	'';
//	var $url			=	'';
//	var $ordering		=	0;
//	var $sourceUrl		=	'';
//	var $cdate			=	'';
//	var $mdate			=	'';
//	var $state			=	0;
//	var $params			=	'';
function mosInsertImage($obj_image)
{
	JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
	$row =& JTable::getInstance('fmedia','Table');
	
	$db	=	JFactory::getDBO();	
	$date = JFactory::getDate();
	$query	=	"SELECT id FROM `#__fmedia` WHERE sourceUrl = ". $db->quote($obj_image->sourceURL);
	$db->setQuery($query);
	if ($id = $db->loadResult())
	{
		$row->load($id);
	}else {
		$row->cdate		= 	$date->toMySQL();
	}
	$row->mdate		= 	$date->toMySQL();	
	$row->sourceUrl		= 	$obj_image->sourceURL;	
	$row->state		= 	1;	
	$row->store();
}